package com.jiexin.service;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.jiexin.common.RequestHolder;
import com.jiexin.dao.PlmWorkFlowMapper;
import com.jiexin.dao.PlmWorkFlowStatusCriterionMapper;
import com.jiexin.dao.PlmWorkFlowStatusMapper;
import com.jiexin.exception.ParamException;
import com.jiexin.model.PlmWorkFlow;
import com.jiexin.model.PlmWorkFlowStatus;
import com.jiexin.param.WorkFlowStatusParam;
import com.jiexin.util.BeanValidator;
import com.jiexin.util.IpUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Jay_Liu
 * @Description:
 * @Date: Created in 16:59 2018/5/19 0019
 * @Modified By:
 */
@Service
public class PlmWorkFlowStatusService {

    @Resource
    PlmWorkFlowStatusMapper plmWorkFlowStatusMapper;

    @Resource
    PlmWorkFlowMapper plmWorkFlowMapper;

    @Resource
    PlmWorkFlowStatusCriterionMapper criterionMapper;

    public void save(WorkFlowStatusParam param) {
        BeanValidator.check(param);
        //查询名称
        if(checkIsExist(param.getWorkFlowId(), param.getName(), param.getId())){
            throw new ParamException("同一个工作流下不允许存在相同关卡名称");
        }
        PlmWorkFlowStatus workFlowStatus = PlmWorkFlowStatus.builder().name(param.getName()).workFlowId(param.getWorkFlowId())
                .seq(param.getSeq()).remark(param.getRemark()).status(param.getStatus()).build();
        workFlowStatus.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        workFlowStatus.setOperateTime(new Date());
        workFlowStatus.setOperator(RequestHolder.getCurrentUser().getUsername());
        plmWorkFlowStatusMapper.insertSelective(workFlowStatus);
    }

    public void update(WorkFlowStatusParam param) {
        BeanValidator.check(param);
        if(checkIsExist(param.getWorkFlowId(), param.getName(), param.getId())){
            throw new ParamException("同一个工作流下不允许存在相同关卡名称");
        }
        PlmWorkFlowStatus before = plmWorkFlowStatusMapper.selectByPrimaryKey(param.getId());
        Preconditions.checkNotNull(before, "待更新的关卡不存在");
        PlmWorkFlowStatus after = PlmWorkFlowStatus.builder().id(param.getId()).name(param.getName()).workFlowId(param.getWorkFlowId())
                .seq(param.getSeq()).remark(param.getRemark()).status(param.getStatus()).build();
        after.setOperateIp(IpUtil.getRemoteIp(RequestHolder.getCurrentRequest()));
        after.setOperateTime(new Date());
        after.setOperator(RequestHolder.getCurrentUser().getUsername());
        plmWorkFlowStatusMapper.updateByPrimaryKeySelective(after);
    }

    public void delete(int workFlowStatusId) {
        PlmWorkFlowStatus status = plmWorkFlowStatusMapper.selectByPrimaryKey(workFlowStatusId);
        Preconditions.checkNotNull(status, "待删除的关卡不存在");
        if (criterionMapper.countByWorkFlowStatusId(workFlowStatusId) > 0){
            throw new ParamException("无法删除存在关卡标准的关卡");
        }
        plmWorkFlowStatusMapper.deleteByPrimaryKey(workFlowStatusId);
    }

    public List<PlmWorkFlowStatus> getWorkFlowStatusByWorkFlowId(int workFlowId) {
        PlmWorkFlow workFlow = plmWorkFlowMapper.selectByPrimaryKey(workFlowId);
        Preconditions.checkNotNull(workFlow, "当前工作流不存在");
        List<PlmWorkFlowStatus> plmWorkFlowStatusList = plmWorkFlowStatusMapper.getWorkFlowStatusByWorkFlowId(workFlowId);
        if(CollectionUtils.isNotEmpty(plmWorkFlowStatusList)){
            return plmWorkFlowStatusList;
        }
        return Lists.newArrayList();
    }

    //检查是否存在相同名称的关卡名称
    private boolean checkIsExist(Integer workFlowId, String name, Integer id) {
        return plmWorkFlowStatusMapper.countByNameAndWorkFlowId(workFlowId, name, id) > 0;
    }
}
